<template>
    <div class="mainDiv">
        <el-row class="rowHead">
            <el-button-group>
                <el-button icon="el-icon-plus" @click="addcost(1)" v-qx="quanxian.ru">收入</el-button>
                <el-button icon="el-icon-minus" @click="addcost(2)" v-qx="quanxian.chu">支出</el-button>
                <el-button icon="el-icon-edit" @click="addcost(3)" v-qx="quanxian.cate">收支分类</el-button>
            </el-button-group>
            <el-button type="primary" icon="el-icon-h-shuaxin1" style="float:right;margin-left:5px" @click="getList">刷新列表</el-button>
        </el-row>
        <div class="rowHead">
            <el-date-picker v-model="page.times" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2" @change="getList">
            </el-date-picker>
            <el-input style="width:300px; float: right" placeholder="搜索收支分类" v-model="page.searchstr" @keyup.enter.native="getList">
                <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getList"></i>
            </el-input>
        </div>
        <el-table v-loading="uploads" element-loading-text="玩命加载中..." :data="tables" :stripe="true" height="100%">
            <el-table-column type="index" label="#" :index="cIndex"></el-table-column>
            <el-table-column prop="typename" label="收支分类"></el-table-column>
            <el-table-column prop="accountname" label="收支账号"></el-table-column>
            <el-table-column prop="type" label="收支类型">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.type==1?'success':'primary'" size="mini">
                        {{scope.row.type==1?'收入':'支出'}}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="costmoney" label="收支金额"></el-table-column>
            <el-table-column prop="remarks" label="收支描述" show-overflow-tooltip></el-table-column>
            <el-table-column prop="fydate" label="添加日期">
                <template slot-scope="scope">
                    {{scope.row.fydate|hourTime}}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-tooltip content="删除" placement="left" :open-delay="800" v-qx="quanxian.del">
                        <el-button icon="el-icon-delete" type="danger" size="mini" @click="delfy(scope.row)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" :current-page="page.pages" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
import Cost from '@/service/shop/cost.js'

export default {
  name: 'costList',
  data () {
    return {
      tables: [],
      total: 1,
      uploads: true,
      page: {
        searchstr: '',
        limits: 15, // 每页显示条数(默认)
        pages: 1,
        times: [new Date(), new Date()]
      },
      quanxian: {
        ru: 'cost_ru',
        chu: 'cost_chu',
        cate: 'cost_cate',
        del: 'cost_del'
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    sizeChange (val) {
      this.page.limits = val
      this.getList()
    },
    currentChange (val) {
      this.page.pages = val
      this.getList()
    },
    cIndex (index) {
      return (this.page.pages - 1) * this.page.limits + index + 1
    },
    addcost: async function (tp) {
      if (tp === 1 || tp === 2) {
        let res = await Cost.openAddCost({ type: tp })
        if (res) {
          C.success(res.data.info)
          this.getList()
        }
      } else {
        C.openBrowserWindow('/costType', {}, { width: 900, height: 750, useSpa: false, spaModel: 'shop', transparent: true, frame: false })
      }
    },
    delfy: function (row) {
      C.confirm('确定删除当前费用单？', async () => {
        let res = await C.ajax('/shop/cost/deletecostinfo', { id: row.id, type: row.type }, 'post')
        if (res) {
          this.getList()
          C.success(res.data)
        }
      })
    },
    getList: async function () {
      let res = await C.ajax('/shop/cost/getcostlist', { page: JSON.stringify(this.page) }, 'post')
      if (res) {
        this.tables = res.data
        this.total = res.count
        this.uploads = false
      }
    }
  },
  mounted: function () {
    this.getList()
  }
}
</script>